package com.example.widget

import android.content.Context
import android.graphics.Bitmap
import android.util.AttributeSet
import android.util.Log
import com.tencent.smtt.export.external.interfaces.ConsoleMessage
import com.tencent.smtt.export.external.interfaces.JsPromptResult
import com.tencent.smtt.export.external.interfaces.JsResult
import com.tencent.smtt.export.external.interfaces.WebResourceResponse
import com.tencent.smtt.sdk.WebChromeClient
import com.tencent.smtt.sdk.WebSettings
import com.tencent.smtt.sdk.WebView
import com.tencent.smtt.sdk.WebViewClient

/**
 * 自定义X5内核的WebView
 **/

class XSWebView constructor(context : Context,attrs: AttributeSet) :WebView(context,attrs) {
   init {
       this.webViewClient=X5WebViewClient()
       this.webChromeClient=X5WebChromeClient()
       initX5WebView()
   }
    private fun initX5WebView(){
        //设置基本配置
        var webSettings =this.settings
        webSettings.javaScriptEnabled=true
        webSettings.allowFileAccess=true
        webSettings.loadsImagesAutomatically=true
        webSettings.javaScriptCanOpenWindowsAutomatically=true
        webSettings.layoutAlgorithm=WebSettings.LayoutAlgorithm.NARROW_COLUMNS
        webSettings.setSupportZoom(false)
        webSettings.builtInZoomControls=false
        webSettings.setAppCacheEnabled(true)
        webSettings.cacheMode=WebSettings.LOAD_NO_CACHE
        webSettings.domStorageEnabled=true
        webSettings.setGeolocationEnabled(false)
    }

    /**
     *H5页面加载流程都可以在当前类中拦截
     * */
    inner class X5WebViewClient : WebViewClient(){
        //H5页面开始加载完成
        override fun onPageStarted(p0: WebView?, p1: String?, p2: Bitmap?) {
            super.onPageStarted(p0, p1, p2)
        }
        //H5页面关闭了
        override fun onPageFinished(p0: WebView?, p1: String?) {
            super.onPageFinished(p0, p1)
        }

        //拦截H5页面中所有请求URL
        override fun shouldOverrideUrlLoading(view: WebView?, url: String?): Boolean {
            view?.loadUrl(url)
            return true
        }
//        //拦截H5页面中所有请求
//        override fun shouldInterceptRequest(p0: WebView?, p1: String?): WebResourceResponse {
//            return super.shouldInterceptRequest(p0, p1)
//        }
    }

    /**
     *控制H5页面中所有的交互
     **/
    inner class X5WebChromeClient : WebChromeClient(){
        //H5页面加载进度
        override fun onProgressChanged(p0: WebView?, p1: Int) {
            super.onProgressChanged(p0, p1)
        }
        //页面关闭
        override fun onCloseWindow(p0: WebView?) {
            super.onCloseWindow(p0)
        }

        override fun onJsAlert(p0: WebView?, p1: String?, p2: String?, p3: JsResult?): Boolean {
            return super.onJsAlert(p0, p1, p2, p3)
        }

        override fun onJsConfirm(p0: WebView?, p1: String?, p2: String?, p3: JsResult?): Boolean {
            return super.onJsConfirm(p0, p1, p2, p3)
        }

        override fun onJsPrompt(p0: WebView?, p1: String?, p2: String?, p3: String?, p4: JsPromptResult?): Boolean {
            return super.onJsPrompt(p0, p1, p2, p3, p4)
        }

        override fun onJsBeforeUnload(p0: WebView?, p1: String?, p2: String?, p3: JsResult?): Boolean {
            return true
        }

        //获取H5页面控制台打印信息
        override fun onConsoleMessage(p0: ConsoleMessage): Boolean {
            Log.i("X5WebChromeClient", "onConsoleMessage: ${p0.message()}")
            return true
        }
    }
}